<?php

/**
 * This is the model class for table "club".
 *
 * The followings are the available columns in table 'club':
 * @property integer $idClub
 * @property string $dataAlta
 * @property string $nom
 * @property string $domicili
 * @property string $codiPostal
 * @property string $poblacio
 * @property string $provincia
 * @property string $CIF
 * @property integer $telFix
 * @property integer $telMobil
 * @property string $eMail
 * @property string $rebreMail
 * @property string $web
 * @property string $directorNom
 * @property integer $directorMobil
 * @property string $directorEMail
 *
 * The followings are the available model relations:
 * @property Esportista[] $esportistas
 * @property Llicencia[] $llicencias
 * @property Usuari[] $usuaris
 */
class Club extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Club the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'club';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('dataAlta, nom, domicili, codiPostal, poblacio, provincia, CIF', 'required'),
			array('telFix, telMobil, directorMobil', 'numerical', 'integerOnly'=>true),
            array('dataAlta', 'type', 'type'=>'date', 'message'=>'<b>{attribute}</b>: Si us plau, introdueix la data amb el format "dd/mm/aaaa".', 'dateFormat'=>Yii::app()->locale->dateFormat),
			array('nom, domicili, poblacio, provincia, eMail, web, directorNom, directorEMail', 'length', 'max'=>45),
            array('codiPostal', 'length', 'min'=>5, 'max'=>5),
			array('CIF', 'length', 'max'=>9),
            array('eMail', 'email'),
            array('web', 'url'),
            array('rebreMail', 'length', 'max'=>1),
            array('directorEMail', 'email'),
            array('nom', 'unique', 'message'=>'Ja existeix un club anomenat <b>{value}</b>.'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idClub, dataAlta, nom, domicili, codiPostal, poblacio, provincia, CIF, telFix, telMobil, eMail, rebreMail, web, directorNom, directorMobil, directorEMail', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'clubEsportistes' => array(self::HAS_MANY, 'Esportista', 'idClub'),
			'clubLlicencies' => array(self::HAS_MANY, 'Llicencia', 'idClub'),
            'esportistesComptador' => array(self::STAT, 'Esportista', 'idClub'),
            'clubUsuaris' => array(self::HAS_MANY, 'Usuari', 'idClub'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idClub' => 'Codi',
			'dataAlta' => "Data d'alta",
			'nom' => 'Nom del club',
			'domicili' => 'Domicili',
			'codiPostal' => 'Codi postal',
			'poblacio' => 'Població',
			'provincia' => 'Provincia',
			'CIF' => 'CIF/NIF',
			'telFix' => "Telèfon fix",
			'telMobil' => "Telèfon mòbil",
			'eMail' => 'Correu electrònic',
            'rebreMail' => 'Rebre correus electrònics',
			'web' => 'Pàgina web',
			'directorNom' => 'Nom i cognoms director/a',
			'directorMobil' => 'Telèfon mòbil director/a',
			'directorEMail' => 'Correu electrònic director/a',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idClub',$this->idClub);
		$criteria->compare('dataAlta',$this->dataAlta,true);
		$criteria->compare('nom',$this->nom,true);
		$criteria->compare('domicili',$this->domicili,true);
		$criteria->compare('codiPostal',$this->codiPostal);
		$criteria->compare('poblacio',$this->poblacio,true);
		$criteria->compare('provincia',$this->provincia,true);
		$criteria->compare('CIF',$this->CIF,true);
		$criteria->compare('telFix',$this->telFix);
		$criteria->compare('telMobil',$this->telMobil);
		$criteria->compare('eMail',$this->eMail,true);
        $criteria->compare('rebreMail',$this->rebreMail,true);
		$criteria->compare('web',$this->web,true);
		$criteria->compare('directorNom',$this->directorNom,true);
		$criteria->compare('directorMobil',$this->directorMobil);
		$criteria->compare('directorEMail',$this->directorEMail,true);
        
        
		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
            'sort'=>array('defaultOrder'=>'nom'),
            'pagination'=>array(
                'pageSize'=>Yii::app()->params['itemsPagina']),

		));
	}
    
    protected function beforeSave()
    {   
        foreach($this->metadata->tableSchema->columns as $columnName => $column){
                if ($column->dbType == 'date'){
                        $this->$columnName = date('Y-m-d', CDateTimeParser::parse($this->$columnName, Yii::app()->locale->dateFormat));
                }elseif ($column->dbType == 'datetime'){
                        $this->$columnName = date('Y-m-d H:i:s', CDateTimeParser::parse($this->$columnName, Yii::app()->locale->dateFormat));
                }
        
        }
        
        return true;
    }
    
    protected function afterFind()
    {
       foreach($this->metadata->tableSchema->columns as $columnName => $column){
                        
                if (!strlen($this->$columnName)) continue;
                        
                if ($column->dbType == 'date'){                         
                        $this->$columnName = Yii::app()->dateFormatter->formatDateTime(
                                        CDateTimeParser::parse($this->$columnName, 'yyyy-MM-dd'),'medium',null);
                        }elseif ($column->dbType == 'datetime'){
                                $this->$columnName = Yii::app()->dateFormatter->formatDateTime(
                                        CDateTimeParser::parse($this->$columnName, 'yyyy-MM-dd hh:mm:ss'));
                        }
        }
        return true;
    }
}